package com.ssy.lingxi.member.merchant.serviceimpl.mobile;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.entity.MemberDO;
import com.ssy.lingxi.member.merchant.entity.MemberRelationDO;
import com.ssy.lingxi.member.merchant.entity.MemberUserDO;
import com.ssy.lingxi.member.merchant.model.constant.MemberUserAuthTypeEnum;
import com.ssy.lingxi.member.merchant.model.vo.basic.request.MemberLogoVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.MemberRegisterTagVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.MobileRegisterTagVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.UserDetailVO;
import com.ssy.lingxi.member.merchant.model.vo.manage.request.MemberAndRoleIdVO;
import com.ssy.lingxi.member.merchant.repository.MemberRelationRepository;
import com.ssy.lingxi.member.merchant.repository.MemberRepository;
import com.ssy.lingxi.member.merchant.repository.MemberUserRepository;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberCacheService;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberRegisterDetailService;
import com.ssy.lingxi.member.merchant.service.mobile.IMobileCommonBusinessService;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * App - 会员其他业务接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-02-03
 */
@Service
public class MobileCommonBusinessServiceImpl implements IMobileCommonBusinessService {

    @Resource
    private IBaseMemberCacheService memberCacheService;

    @Resource
    private MemberRelationRepository relationRepository;

    @Resource
    private IBaseMemberRegisterDetailService memberRegisterDetailService;

    @Resource
    private IBaseMemberRegisterDetailService baseMemberRegisterDetailService;

    @Resource
    private MemberRepository memberRepository;

    @Resource
    private MemberUserRepository memberUserRepository;

    /**
     * 新增或修改用户Logo
     *
     * @param headers Http头部信息
     * @param logoVO  接口参数
     * @return 操作结果
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public Wrapper<Void> addMemberUserLogo(HttpHeaders headers, MemberLogoVO logoVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromMobile(headers);

        MemberUserDO memberUserDO = memberUserRepository.findById(loginUser.getUserId()).orElse(null);
        if(memberUserDO == null || memberUserDO.getMember() == null || !memberUserDO.getMember().getId().equals(loginUser.getMemberId())) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_USER_DOES_NOT_EXIST);
        }

        memberUserDO.setLogo(logoVO.getLogo());
        memberUserRepository.saveAndFlush(memberUserDO);

        if(memberUserDO.getTypeEnum().equals(MemberUserAuthTypeEnum.ADMIN.getCode())) {
            MemberDO memberDO = memberUserDO.getMember();
            memberDO.setLogo(logoVO.getLogo());
            memberRepository.saveAndFlush(memberDO);
        }

        return Wrapper.success();
    }

    /**
     * 查询会员标签注册资料
     * @param headers Http头部信息
     * @param idVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MobileRegisterTagVO> getMemberRegisterTagDetail(HttpHeaders headers, MemberAndRoleIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromMobile(headers);

        Wrapper<MemberRegisterTagVO> result = memberRegisterDetailService.getMemberRegisterTagDetail(idVO.getMemberId());
        if(result.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(result.getCode(), result.getMessage());
        }

        MobileRegisterTagVO tagVO = new MobileRegisterTagVO();
        BeanUtils.copyProperties(result.getData(), tagVO);

        MemberRelationDO relationDO = relationRepository.findFirstByMemberIdAndRoleIdAndSubMemberIdAndSubRoleId(idVO.getMemberId(), idVO.getRoleId(), loginUser.getMemberId(), loginUser.getMemberRoleId());
        if(relationDO == null) {
            final Integer isNotSubMember = -1;
            tagVO.setOuterStatus(isNotSubMember);
        } else {
            tagVO.setOuterStatus(relationDO.getOuterStatus());
        }

        return Wrapper.success(tagVO);
    }

    /**
     * 查询用户注册资料
     *
     * @param headers Http头部信息
     * @return 查询结果
     */
    @Override
    public Wrapper<UserDetailVO> getUserDetail(HttpHeaders headers) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromMobile(headers);
        return baseMemberRegisterDetailService.getUserDetail(loginUser.getUserId());
    }
}
